<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生日" prop="birth">
        <el-date-picker
          clearable
          v-model="queryParams.birth"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择生日"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="生肖" prop="zodiac">
        <el-input
          v-model="queryParams.zodiac"
          placeholder="请输入生肖"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入产品名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="status">
        <el-date-picker
          v-model="date"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      border
      v-loading="loading"
      :data="videosList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex" width="100" />
      <el-table-column label="生日" align="center" prop="birth" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birth, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生肖" align="center" prop="zodiac" />
      <el-table-column label="产品名" align="center" prop="goodsName" />
      <el-table-column label="视频/文本" align="center" prop="url" width="220">
        <template slot-scope="scope">
          <video
            v-if="scope.row.goodsType == 0"
            :src="scope.row.url"
            controls
            width="200"
            height="100"
          ></video>
          <view v-else class="">
            {{ scope.row.url }}
          </view>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="payPrice" />
      <el-table-column
        label="下单时间"
        align="center"
        prop="createTime"
        min-width="100"
      />
      <el-table-column
        label="收货地址"
        align="center"
        prop="text1"
        min-width="150"
      />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.status == 0">待上传</el-tag>
          <el-tag type="success" v-else-if="scope.row.status == 1"
            >已上传</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['api:videos:edit']"
            >上传</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['api:videos:edit']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          {{ form.name }}
        </el-form-item>
        <el-form-item label="生日" prop="birth">
          {{ form.birth }}
        </el-form-item>
        <el-form-item label="生肖" prop="zodiac">
          {{ form.zodiac }}
        </el-form-item>
        <el-form-item label="产品名" prop="goodsName">
          {{ form.goodsName }}
        </el-form-item>
        <el-form-item label="视频" prop="url" v-if="form.goodsType == 0">
          <file-upload v-model="form.url" />
        </el-form-item>

        <el-form-item label="输入文本" prop="url" v-else>
          <el-input v-model="form.url" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listVideos,
  getVideos,
  delVideos,
  addVideos,
  updateVideos,
} from "@/api/api/videos";

export default {
  name: "Videos",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      options: [
        {
          value: "1",
          label: "已上传",
        },
        {
          value: "0",
          label: "未上传",
        },
      ],
      // 视频表格数据
      videosList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sex: null,
        birth: null,
        zodiac: null,
        goodsName: null,
        url: null,
        status: null,
        userId: null,
        text1: null,
        status: "",
  
      },
            date:'',
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询视频列表 */
    getList() {
      this.loading = true;
      if (this.date) {
        this.queryParams.startDate = this.date[0];
        this.queryParams.endDate = this.date[1];
      } else {
        this.queryParams.startDate = '';
        this.queryParams.endDate = '';
      }
      listVideos(this.queryParams).then((response) => {
        this.videosList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
   
      this.form = {
        id: null,
      }
      listVideos(this.queryParams).then((response) => {
        this.videosList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sex: null,
        birth: null,
        zodiac: null,
        goodsName: null,
        url: null,
        status: null,
        userId: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        text1: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.date = ''
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加视频";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getVideos(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "上传视频";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.status = "1";
            updateVideos(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form.status, "asdasdasd");
            addVideos(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除视频编号为"' + ids + '"的数据项？')
        .then(function () {
          return delVideos(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "api/videos/export",
        {
          ...this.queryParams,
        },
        `videos_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
